package com.example.yangxu0703.thundermapclient.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Set;

public class Clientbluetooth extends AbsBluetooth implements AbsBluetooth.Listerner{

    public BluetoothAdapter bluetoothAdapter;
    private BluetoothDevice dev;
    private boolean isSending = false;
    private OutputStream outstream;
    private static Clientbluetooth clientbluetooth;
    private Listerner listerner;
    public ConnectThread connectThread;

    public static Clientbluetooth getinstance() {
        if (clientbluetooth == null) {
            clientbluetooth = new Clientbluetooth();
        }
        return clientbluetooth;
    }

    public void configBle() {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter != null) {
            if (!bluetoothAdapter.isEnabled()) {
                bluetoothAdapter.enable();
            }
            Set<BluetoothDevice> allDevice = bluetoothAdapter.getBondedDevices();
            for (BluetoothDevice device : allDevice) {
                dev = device;
                Log.i("CON_STATE", device.getName());
                // 获取到一个device对象，去连接。。
            }
        }

    }

    @Override
    public void sendMsg(String msg) throws IOException {
        /*阻塞去确保蓝牙已连接
         * */
        Log.i("MSG",msg+"-------");
        configBle();
        if (dev!=null){
            if (connectThread==null){
                connectThread = new ConnectThread(bluetoothAdapter, dev);
                connectThread.start();
            }
            connectThread.setMsg(msg);
            //connectThread.start();
            connectThread.setListerner(this);
        }
    }

    public  void SendFile(File file){
        configBle();
        if (dev!=null){
            if (connectThread==null){
                connectThread = new ConnectThread(bluetoothAdapter, dev);
                connectThread.start();
            }
            connectThread.setFile(file);
            connectThread.setListerner(this);
        }
    }


    @Override
    public void startConnect() {
        configBle();
        if (dev != null) {
            if (connectThread==null){
                connectThread = new ConnectThread(bluetoothAdapter, dev);
                connectThread.start();
            }
            connectThread.setMsg("开始连接");
            connectThread.setListerner(this);
        }

    }

    @Override
    public void waitConnect() {
        AccpetThread accpetThread = new AccpetThread(BluetoothAdapter.getDefaultAdapter());
        accpetThread.start();
    }


    public void setListerner(Listerner listerner) {
        this.listerner = listerner;
    }


    @Override
    public void observerConnection(int status) {
        listerner.observerConnection(status);

    }

    @Override
    public void backMessage(String msg) {
        listerner.backMessage(msg);
    }
}
